from django.shortcuts import render
from rest_framework.views import APIView
# Create your views here.
from rest_framework.response import Response
from .sers import *
from rest_framework.generics import GenericAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.mixins import ListModelMixin,CreateModelMixin

class CateOther(PageNumberPagination):
    page_size = 3
    def get_paginated_response(self, data):
        resp={}
        resp['shang_page']=self.page.number-1
        resp['xia_page']=self.page.number+1
        resp['num_list']=[i for i in self.page.paginator.page_range]
        resp['data']=data
        return Response(resp)

class CateView(GenericAPIView,ListModelMixin,CreateModelMixin):
    queryset = CateModel.objects.all()
    serializer_class = CateSer
    pagination_class = CateOther
    def get(self,request):
        return self.list(request)
    def post(self,request):
        return self.create(request)

class GoodsView(APIView):
    def get(self, request):
        id = request.GET.get('id')
        print(id+'-------------------------------')
        queryset = GoodsModel.objects.filter(cate_id=id)
        ser = GoodsSer(instance=queryset, many=True)
        return Response(ser.data)

    def post(self,request):
        ser = GoodsSer(data=request.data)
        resp={}
        if ser.is_valid():
            ser.save()
            resp['code']=200
            resp['msg']='添加成功'
        else:
            resp['code']=400
            resp['msg']='添加失败'
        return Response(resp)
    # def get(self,request):
    #     quest = CateModel.objects.all()
    #     ser = CateSer(quest,many=True)
    #     return Response(ser.data)
    #
    # def post(self,request):
    #     ser = CateSer(data=request.data)
    #     resp={}
    #     if ser.is_valid():
    #         ser.save()
    #         resp['code']=200
    #         resp['msg']='添加成功'
    #     else:
    #         resp['code']=400
    #         resp['msg']='添加失败'
    #     return Response(resp)